package com.stars.empoly.utils

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton
import com.stars.empoly.R
import com.stars.empoly.adapter.AccountTypeAdapter
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.inte.ChoiceListData
import com.stars.empoly.model.bean.AccountTypeBean

/**
 * 选择类型
 *Created by shihuiyun
 *on 2020/9/21
 */
object  PopuChoseTypeUtils {

    fun show(context: Context, view: View, choiceData: ChoiceListData){

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.popu_chose_type, null)

        val ll:LinearLayout=layout.findViewById(R.id.ll)
        val imgClose:ImageView=layout.findViewById(R.id.img_close)

        val butSure:QMUIRoundButton=layout.findViewById(R.id.but_sure)


        //设置数据
        var list=ArrayList<AccountTypeBean>()
        list.add(AccountTypeBean("全部","0",false))
        list.add(AccountTypeBean("充值","1",false))
        list.add(AccountTypeBean("消费","2",false))
//        list.add(AccountTypeBean("提现","3",false))
//        list.add(AccountTypeBean("收入","4",false))

        var typeId=""
        var typeName=""


        val recyclerType: RecyclerView =layout.findViewById(R.id.recycler_type)
        var adapter= AccountTypeAdapter()
        recyclerType.adapter=adapter
        adapter.addData(list)
        adapter.setOnItemClickListener { adapter, view, position ->

            var list: ArrayList<AccountTypeBean> = adapter.data as ArrayList<AccountTypeBean>

            for ((index, value) in list.withIndex()) {

                if (index == position) {
                    typeId=value.id
                    typeName=value.name
                    value.chose = true
                    adapter.notifyItemChanged(position)

                } else {
                    if (value.chose) {
                        value.chose = false
                        adapter.notifyItemChanged(position)
                    }

                }
            }


        }
        butSure.onClick {
            if(typeId.isNullOrEmpty()){
                Toast.makeText(context,"请选择",Toast.LENGTH_SHORT).show()
                return@onClick
            }
            if(null!=choiceData){
                choiceData.onRestuse(typeId,typeName)
                mNormalPopup.dismiss()

            }
        }






        imgClose.onClick {
            mNormalPopup.dismiss()
        }
        ll.onClick {

        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
            height)
        frameLayout.addView(layout, lp)


        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
        mNormalPopup.show(view)


    }




}
